package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatchatbackend.entity.dto.Adv;
import com.example.chatchatbackend.entity.dto.Comments;
import com.example.chatchatbackend.entity.dto.Comments;
import com.example.chatchatbackend.entity.vo.request.CommentsVO;
import com.example.chatchatbackend.mapper.CommentsMapper;
import com.example.chatchatbackend.service.CommentsService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private CommentsMapper commentsMapper;
    @Override
    public List<CommentsVO> getAllComments() {
          return commentsMapper.getAll();
    }

    @Override
    public CommentsVO insertComments(CommentsVO commentsVO) {
        Comments comment = new Comments(null,commentsVO.getProductId(),commentsVO.getUserId(),commentsVO.getComment(),this.currentTime,this.currentTime,commentsVO.getStatus());
        if(this.save(comment))
            return commentsVO;
        return null;
    }

    @Override
    public List<CommentsVO> insertCommentsList(List<CommentsVO> commentsVOList) {

        List<Comments> commentsList = new ArrayList<>();

        for (CommentsVO commentsVO : commentsVOList) {
            commentsList.add(new Comments(null,commentsVO.getProductId(),commentsVO.getUserId(),commentsVO.getComment(),this.currentTime,this.currentTime,commentsVO.getStatus()));
        }
        commentsMapper.insertList(commentsList);
        return commentsVOList;
    }


    @Override
    public IPage<Comments> pageThree(Integer pageNum, Integer pageSize) {
        Page<Comments> page = new Page<>(pageNum, pageSize);
        IPage<Comments> commentsIPage = commentsMapper.selectAll(page, null);
        return commentsIPage;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        commentsMapper.deleteBatchIds(ids);
    }

}
